# ~*~ coding: utf-8 ~*~
import json

from flask_wtf import FlaskForm
from wtforms import StringField

from apps.common.form import ErrorFormMinx

__all__ = ['InventoryGroupForm', 'InventoryHostVariableForm']


class InventoryGroupForm(ErrorFormMinx, FlaskForm):
    name = StringField()
    asset_ids = StringField()
    vars = StringField()
    memo = StringField()

    @property
    def data(self):
        return {
            'name': self.name.data,
            'asset_ids': self.asset_ids.data,
            'vars': json.loads(self.vars.data) if self.vars.data else json.loads({}),
            'memo': self.memo.data,
        }


class InventoryHostVariableForm(ErrorFormMinx, FlaskForm):
    asset_id = StringField()
    vars = StringField()

    @property
    def data(self):
        return {
            'asset_id': self.asset_id.data,
            'vars': json.loads(self.vars.data) if self.vars.data else json.loads({}),
        }
